package com.oascarmovie;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.oascarmovie.widget.MovieResultWidget;

public class SearchActivity extends Activity {
  private Button keywordSearchButton;
  private Button catalogSearchButton;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    loadWidget();
    build();
    //render(null);
  }

  private void switchToCatalogSearchView() {
    Intent intent = new Intent();
    intent.setClass(SearchActivity.this, CatalogSearchActivity.class);
    startActivity(intent);
  }
  
  /*int counter = 0;
  private void addTableRow() {
	// get a reference for the TableLayout  
      TableLayout table = (TableLayout) findViewById(R.id.table_result);  
 
      // create a new TableRow  
      TableRow row = new TableRow(this);  
 
      // count the counter up by one  
      counter++;  
 
      // create a new TextView  
      TextView t = new TextView(this);  
      // set the text to "text xx"  
      t.setText("text " + counter);  
 
      // create a CheckBox  
      CheckBox c = new CheckBox(this);  
 
      // add the TextView and the CheckBox to the new TableRow  
      row.addView(t);  
      row.addView(c);  
 
      // add the TableRow to the TableLayout  
      table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
  }*/

  protected void loadWidget() {
	  this.catalogSearchButton = (Button) findViewById(R.id.button_catalog);
	  this.keywordSearchButton = (Button) findViewById(R.id.button_kewyword_search);
  }

  protected void render(Bundle bundle) {
	TableRow tableRow = null;
	
    for (int i = 0; i < 15; i++) {
    	TableLayout tableLayout = (TableLayout)findViewById(R.id.table_result);
    	if (i % 7 == 0) {
    		tableRow = new TableRow(this);
    		TableLayout.LayoutParams params = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    		tableRow.setLayoutParams(params);
    		tableLayout.addView(tableRow);
    	}
    	if (tableRow != null) {
            MovieResultWidget widget = new MovieResultWidget(this);
            tableRow.addView(widget);
    	}
    }
  }

  protected void build() {
    this.catalogSearchButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
    	  switchToCatalogSearchView();
      }
    });
  }
}